﻿using System;
using System.Data.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace AbpProjectTemplate.EntityFrameworkCore
{
    public static class AppDbContextConfigurer
    {
        /// <summary>
        /// 注册迁移相关的DbContext
        /// </summary>
        public static void AddAppDbContext(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddDbContext<AppDbContext>(optionsBuilder =>
            {
                Configure(optionsBuilder, configuration.GetConnectionString(AppConsts.ConnectionStringName));
            });
        }

        public static void Configure(DbContextOptionsBuilder builder, string connectionString)
        {
            builder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
        }

        public static void Configure(DbContextOptionsBuilder builder, DbConnection connection)
        {
            builder.UseMySql(connection, ServerVersion.AutoDetect(connection.ConnectionString), o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
        }
    }
}

